package com.paw.portal.util;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.paw.common.domain.IBaseEntity;
import com.paw.common.domain.PageRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

/**
 * @author Rubble
 * @date 2021/8/24
 **/
@Slf4j
public class QueryWrapperUtil {

  public static <T extends PageRequest,M extends IBaseEntity> LambdaQueryWrapper<M> buildLambdaQueryWrapper (T requestDto,Class<M> entityKlass){
    try {
      M condition = entityKlass.newInstance();
      BeanUtils.copyProperties(requestDto,condition);
      LambdaQueryWrapper<M> queryWrapper = Wrappers.<M>lambdaQuery(condition)
          .ge(requestDto.getCreateTimeStart()!=null,M::getCreateTime,requestDto.getCreateTimeStart())
          .lt(requestDto.getCreateTimeEnd()!=null,M::getCreateTime,requestDto.getCreateTimeEnd());
      return queryWrapper;
    } catch (InstantiationException e) {
      log.error("buildQueryWrapperUtil exception ",e);
      throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
      log.error("buildQueryWrapperUtil exception ",e);
      throw new RuntimeException(e);
    }
  }

}
